#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
    const int mod = 1337;

public:
    int superPow(int a, vector<int>& b)
    {
        int ans = 1;
        for (const int x : b)
        {
            ans = (_pow(ans, 10) * _pow(a, x)) % mod;
        }
        return ans;
    }

private:
    int _pow(int a, int x)
    {
        if (x == 0) return 1;
        else if (x == 1) return a % mod;

        int half = _pow(a, x / 2);
        if (x % 2 == 0)
        {
            return (half * half) % mod;
        }
        else
        {
            return ((a % mod) * (half * half % mod)) % mod;
        }
    }
};